package com.csthink.common.infrastructure.util;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-12
 */
public class StringUtil {
    public static String getCSVStringWithProtection(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        } else {
            char firstChar = str.charAt(0);
            if (firstChar < ' ' || firstChar == '+' || firstChar == '-' || firstChar == '=' || firstChar == '@') {
                str = "'".concat(str);
            }

            return getCSVString(str);
        }
    }

    public static String getCSVStringExport(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        } else {
            return getCSVString(str);
        }
    }

    public static String getCSVString(String str) {
        if (str == null) {
            return "";
        } else {
            if (str.startsWith("\"") || str.endsWith("\"") || str.contains(",") || str.contains("\r") || str.contains("\n")) {
                str = str.replace("\"", "\"\"");
                str = "\"" + str + "\"";
            }

            return str;
        }
    }

    public static String formatEnglishName(String name){
        if(StringUtils.isBlank(name))
            return name;
        String[] arr=name.split(" +");
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for(String s:arr){
            char[] cs=s.toCharArray();
            char c = cs[0];
            cs[0] = c >= 'a' && c <= 'z' ? (char) (c ^ 32) : c;
            sb.append(cs);
            if (i < arr.length - 1) {
                sb.append(' ');
            }
            i++;
        }
        return sb.toString();
    }

    public static Set<String> splitToSet(String str) {
        return splitToSet(str, ",");
    }

    public static List<String> splitToList(String str) {
        return splitToList(str, ",");
    }

    public static Set<String> splitToSet(String str, String separatorChars) {
        if (StringUtils.isEmpty(str)) {
            return new HashSet<>();
        }
        return Arrays.stream(StringUtils.split(str, separatorChars))
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toSet());
    }

    public static List<String> splitToList(String str, String separatorChars) {
        if (StringUtils.isEmpty(str)) {
            return new ArrayList<>();
        }
        return Arrays.stream(StringUtils.split(str, separatorChars))
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());
    }

    public static String uuid(String seed) {
        if (StringUtils.isBlank(seed)) {
            return UUID.randomUUID().toString();
        }

        return UUID.nameUUIDFromBytes(seed.getBytes()).toString();
    }

    public static String replaceAll(String originalString, String regex, String replacement){
        return Pattern.compile(regex).matcher(originalString).replaceAll(replacement);
    }
}
